home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d16 / scrldlg.arc / SCROLL.C < prev    next >
Text File  |  1991-05-02  |  6KB  |  196 lines

  1. /****************************************************************************
  2. *  MODULE      :  scroll.c
  3. *  AUTHOR      :  Steven B. Katz
  4. *  DATE        :  30-NOV-1990
  5. *
  6. *  DESCRIPTION :  This program is sample code to demonstrate how to create
  7. *                 a scrollable dialog box.
  8. *
  9. *  If you have any questions, I can be reached on Compuserve through 
  10. *  Bill Gutches, 76477,3044
  11. *  
  12. ****************************************************************************/
  13. #include "windows.h"
  14. #include "scroll.h"
  15. #include "scrdef.h"  
  16.  
  17. /*
  18. These values are used for calculating the scroll position of the dialog
  19. box.  These values were calculated by trial and error and will be different
  20. for each dialog box.  There is probably a way to calculate them, however for
  21. this example I just hard coded them.  If this was to be a resizeable
  22. scrollable dialog box, these values would have to be calulated dynamically.
  23. */
  24. #define SCROLLMAX 130
  25. #define SCROLLINC 10
  26.  
  27. HANDLE hInst;
  28.  
  29. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  30. HANDLE hInstance;              
  31. HANDLE hPrevInstance;        
  32. LPSTR lpCmdLine;              
  33. int nCmdShow;                 
  34. {
  35.     MSG msg;                 
  36.  
  37.     if (!hPrevInstance)   
  38.         if (!InitApplication(hInstance))
  39.             return (FALSE);
  40.  
  41.     if (!InitInstance(hInstance, nCmdShow))
  42.         return (FALSE);
  43.  
  44.     while (GetMessage(&msg, NULL, NULL, NULL)) {
  45.         TranslateMessage(&msg);      
  46.         DispatchMessage(&msg);      
  47.     }
  48.     return (msg.wParam);      
  49. }
  50.  
  51.  
  52.  
  53. BOOL InitApplication(hInstance)
  54. HANDLE hInstance;                /* current instance        */
  55. {
  56.     WNDCLASS  wc;
  57.  
  58.     wc.style = NULL;                    
  59.     wc.lpfnWndProc = MainWndProc;       
  60.                                         
  61.     wc.cbClsExtra = 0;                  
  62.     wc.cbWndExtra = 0;                  
  63.     wc.hInstance = hInstance;           
  64.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  65.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  66.     wc.hbrBackground = GetStockObject(WHITE_BRUSH); 
  67.     wc.lpszMenuName =  "ScrollMenu";   
  68.     wc.lpszClassName = "ScrollWClass"; 
  69.  
  70.     return (RegisterClass(&wc));
  71.  
  72. }
  73.  
  74.  
  75.  
  76. BOOL InitInstance(hInstance, nCmdShow)
  77.     HANDLE          hInstance;
  78.     int             nCmdShow; 
  79. {
  80.     HWND            hWnd;     
  81.  
  82.     hInst = hInstance;
  83.  
  84.     hWnd = CreateWindow(
  85.         "ScrollWClass",                
  86.         "Scroll Application",   
  87.         WS_OVERLAPPEDWINDOW,            
  88.         0,                  
  89.         0,                  
  90.         CW_USEDEFAULT,                  
  91.         CW_USEDEFAULT,                  
  92.         NULL,                           
  93.         NULL,                           
  94.         hInstance,                      
  95.         NULL                            
  96.     );
  97.  
  98.     if (!hWnd)
  99.         return (FALSE);
  100.  
  101.     ShowWindow(hWnd, nCmdShow);  
  102.     UpdateWindow(hWnd);          
  103.     return (TRUE);               
  104.  
  105. }
  106.  
  107.  
  108. long FAR PASCAL MainWndProc(hWnd, message, wParam, lParam)
  109. HWND hWnd;              
  110. unsigned message;        
  111. WORD wParam;           
  112. LONG lParam;           
  113. {
  114.     FARPROC lpProcAbout;
  115.  
  116.     switch (message) {
  117.        case WM_COMMAND:
  118.           switch (wParam) {
  119.               case IDM_SCROLL:
  120.                   lpProcAbout = MakeProcInstance(ScrollProc, hInst);
  121.                   DialogBox(hInst,"Scroll", hWnd, lpProcAbout);    
  122.                   FreeProcInstance(lpProcAbout);
  123.                   break;
  124.     
  125.               default:
  126.                   return (DefWindowProc(hWnd, message, wParam, lParam));
  127.           }
  128.           break;
  129.  
  130.        case WM_DESTROY:        /* message: window being destroyed */
  131.           PostQuitMessage(0);
  132.           break;
  133.     
  134.        default:           /* Passes it on if unproccessed    */
  135.            return (DefWindowProc(hWnd, message, wParam, lParam));
  136.     }
  137.     return (NULL);
  138. }
  139.  
  140.  
  141.  
  142. BOOL FAR PASCAL ScrollProc(hDlg, message, wParam, lParam)
  143. HWND hDlg;       
  144. unsigned message;
  145. WORD wParam;     
  146. LONG lParam;
  147. {
  148.     static int iScrollPos;
  149.  
  150.     switch (message) {
  151.        case WM_INITDIALOG:
  152.            iScrollPos = 0;
  153.            SetScrollRange(hDlg,SB_VERT, 0, SCROLLMAX, FALSE);
  154.            SetScrollPos(hDlg,SB_VERT,-iScrollPos,TRUE);
  155.            return (TRUE);
  156.  
  157.        case WM_VSCROLL:
  158.             switch (wParam) {
  159.                 case SB_PAGEUP:
  160.                 case SB_LINEUP:
  161.                    if (iScrollPos == 0)
  162.                       break;
  163.                    ScrollWindow(hDlg, 0, SCROLLINC, NULL,NULL);
  164.                    iScrollPos += SCROLLINC;
  165.                    SetScrollPos(hDlg,SB_VERT,-iScrollPos,TRUE);
  166.                    UpdateWindow(hDlg);
  167.                    break;
  168.  
  169.                 case SB_PAGEDOWN:
  170.                 case SB_LINEDOWN:
  171.                    if (iScrollPos <= -SCROLLMAX)
  172.                       break;
  173.                    ScrollWindow(hDlg, 0, -SCROLLINC, NULL,NULL);
  174.                    iScrollPos -= SCROLLINC;
  175.                    SetScrollPos(hDlg,SB_VERT,-iScrollPos,TRUE);
  176.                    UpdateWindow(hDlg);
  177.                    break;
  178.             }
  179.             break;
  180.  
  181.        case WM_COMMAND:
  182.           switch (wParam) {
  183.                case IDOK:
  184.                   EndDialog(hDlg, TRUE);        
  185.                   return (TRUE);
  186.                   break;
  187.  
  188.                case IDCANCEL:
  189.                   EndDialog(hDlg, FALSE);        
  190.                   return (FALSE);
  191.           }
  192.           break;
  193.     }
  194.     return (FALSE);              
  195. }
  196.